package com.cmcc.volte.xdr.SigXDR;

import java.io.Serializable;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import com.cmcc.volte.xdr.util.MessageUtil;
import com.google.common.hash.HashCode;

public class SigXDR implements Serializable {

	private static final long serialVersionUID = -5666617595900469738L;
	private SigXDRHeader sigXDRheader;
	private List<SigXDRData> sigXDRdata;
	public ByteBuffer buffer;
	String type;
	private int packgeType = 0;//0-IPv4(default),1-IPv6,2-IPv6+ESP，对应SigXDRReassemble的ReassembleAgent的packgeType说明
	private int statusType;

	SigXDR() {

	}

	public SigXDR(ByteBuffer buffer, String type) {
		this.buffer = buffer;
		if("2".equals(type)){
//			//如果是接口过来的数据则不用flip
			//第10个字节result 在传过来就已经取出来了
		}else{
			buffer.flip();
			int result = this.loadInt(1);
			if (result != 1)
				return;
		}
		
		SigXDRHeader sigXDRHeader = new SigXDRHeader();
		sigXDRHeader.setRAT(this.loadByte(1)[0]);
		sigXDRHeader.setInterface(this.loadByte(1)[0]);
		sigXDRHeader.setXDR_ID(this.loadByte(16));
		sigXDRHeader.setSize(this.loadBigInteger(2).shortValue());
		// lengthTotal实际长度为4，这里过路
//		this.loadByte(2);
//		sigXDRHeader.setLengthTotal(this.loadBigInteger(2).shortValue());
		sigXDRHeader.setLengthTotal(this.loadBigInteger(2).intValue());
		
		List<SigXDRData> sigXDRDataList = new ArrayList<SigXDRData>();
		for (int i = 0; i < sigXDRHeader.getSize(); i++) {
			SigXDRData sigXDRData = new SigXDRData();
			sigXDRData.setLen(this.loadBigInteger(2).shortValue());
			sigXDRData.setVer(this.loadByte(1)[0]);
			sigXDRData.setLinkType(this.loadByte(1)[0]);
			sigXDRData.setCardType(this.loadByte(1)[0]);
			sigXDRData.setCardId(this.loadByte(1)[0]);
			sigXDRData.setTime_s(this.loadInt(4));
			sigXDRData.setTime_ns(this.loadInt(4));
			sigXDRData.setLoad(this.loadByte(sigXDRData.getLen() - 12));
			sigXDRDataList.add(sigXDRData);
		}
		this.setHeader(sigXDRHeader);
		this.setData(sigXDRDataList);
	}

	public SigXDRHeader getHeader() {
		return sigXDRheader;
	}

	public void setHeader(SigXDRHeader sigXDRheader) {
		this.sigXDRheader = sigXDRheader;
	}

	public List<SigXDRData> getData() {
		return sigXDRdata;
	}

	public void setData(List<SigXDRData> sigXDRdata) {
		this.sigXDRdata = sigXDRdata;
	}

	public ByteBuffer getBuffer() {
		return buffer;
	}

	public void setBuffer(ByteBuffer buffer) {
		this.buffer = buffer;
	}

	/**
	 * @comment 从buff中读取指定长度的数据,buffer的pos会改变所以需要按顺序读取
	 * @param length
	 * @return
	 */
	public byte[] loadByte(int length) {
		byte[] temp = new byte[length];
		buffer.get(temp);
		return temp;
	}

	/**
	 * @comment 从buff中读取指定长度的数据被将其转换为ip地址。ipv4仅占用四个字节所以将多余的截去。
	 * @param length
	 * @return
	 */
	public String loadIp(int length) {
		return MessageUtil.toIpAddress(loadByte(length));
	}

	/**
	 * @comment 从buff中读取指定长度的数据并转换为long buffer的pos会改变所以需要按顺序读取
	 * @param length
	 * @return
	 */
	public long loadLong(int length) {
		return MessageUtil.toBigInteger(loadByte(length)).longValue();

	}

	/**
	 * @comment 从buff中读取指定长度的数据并转换为int buffer的pos会改变所以需要按顺序读取
	 * @param length
	 * @return
	 */
	public int loadInt(int length) {
		return MessageUtil.toBigInteger(loadByte(length)).intValue();
	}

	/**
	 * @comment 从buff中读取指定长度的数据并转换为string buffer的pos会改变所以需要按顺序读取
	 * @param length
	 * @return
	 */
	public String loadStr(int length) {
		return new String(loadByte(length));
	}

	/**
	 * @comment 从buff中读取指定长度的数据并转换为16进制Hash str, buffer的pos会改变所以需要按顺序读取
	 * @param length
	 * @return
	 */
	public String loadHexStr(int length) {
		return HashCode.fromBytes(loadByte(length)).toString();
	}

	/**
	 * @comment 从buff中读取指定长度的数据,buffer的pos会改变所以需要按顺序读取
	 * @param length
	 * @return
	 */
	public byte[] load(byte[] data, int length) {
		data = loadByte(length);
		return data;
	}

	/**
	 * @comment 从buff中读取指定长度的数据并转换为BigInteger
	 * @param length
	 * @return
	 */
	public BigInteger loadBigInteger(int length) {
		return MessageUtil.toBigInteger(loadByte(length));
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getStatusType() {
		return statusType;
	}

	public void setStatusType(int statusType) {
		this.statusType = statusType;
	}
	public int getPackgeType() {
		return packgeType;
	}
	public void setPackgeType(int packgeType) {
		this.packgeType = packgeType;
	}

}